﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.SqlClient;

namespace Sami_SMS_CRM_Business
{
    public class SubscriberRepository : Repository
    {
        public  IList<crm_subscriber> GetAll(int currentPage, int pageSize, ref int totalRecord)
        {
            var retValue = DB.crm_subscribers.OrderBy(p => p.seq_id);
            totalRecord = retValue.Count();
            return retValue.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
        }

        public  IList<crm_subscriber> GetAll(int currentPage, int pageSize, ref int totalRecord, string subscriber, bool? enable)
        {
            var retValue = DB.crm_subscribers
                                 .Where(p => (string.IsNullOrEmpty(subscriber) ? true : p.subscriber.Contains(subscriber))
                                        && (enable == null ? true : p.enabled == enable))
                                .OrderBy(p => p.seq_id);
            totalRecord = retValue.Count();
            return retValue.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
        }

        public  crm_subscriber GetById(string Id)
        {
            return DB.crm_subscribers.FirstOrDefault(p => p.subscriber == Id);
        }

        public  void DeleteById(string Id)
        {
            var entity = DB.crm_subscribers.FirstOrDefault(p => p.subscriber == Id);
            if (entity != null)
            {
                DB.crm_subscribers.DeleteOnSubmit(entity);
                DB.SubmitChanges();
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.crm_subscribers);
            }
        }

        public  void AddNew(crm_subscriber entity)
        {
            DB.crm_subscribers.InsertOnSubmit(entity);
            DB.SubmitChanges();
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.crm_subscribers);
        }

        public  void Update(crm_subscriber entity)
        {
            DB.SubmitChanges();
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.crm_subscribers);
        }
    }
}
